package org.wll.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wll.dto.DeptEmpDto;
import org.wll.service.DepartmentService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("test")
public class TestController {


    @Autowired
    DepartmentService departmentService;


    /**
     * 查询该部门下的多有员工
     *
     * @return
     */
    @RequestMapping("dept/all/emp")
    public Object findDepAllemp(Integer did) {
        if (did == null) {
            return "参数did为空";
        }
        List<DeptEmpDto> list = departmentService.findDeptAllEmpByDid(did);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "查询成功");
        map.put("data", list);
        return map;
    }


    /**
     * 查询该部门下的多有员工 分页
     *
     * @return
     */
    @RequestMapping("dept/all/emp/page")
    public Object findDepAllempPage(Integer did,
                                    @RequestParam(defaultValue = "1") Integer pageNumber,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        if (did == null) {
            return "参数did为空";
        }
        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        Page<Map> list = departmentService.findDeptAllEmpPageByDid(did, pageable);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "查询成功");
        map.put("data", list);
        return map;
    }


    /**
     * 自定义分页
     *
     * @param did
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("dept/all/emp/page2")
    public Object findDepAllempPage2(Integer did,
                                     @RequestParam(defaultValue = "1") Integer pageNumber,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        if (did == null) {
            return "参数did为空";
        }
        Object obj = departmentService.findDeptAllEmpPage2ByDid(did, pageNumber, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "查询成功");
        map.put("data", obj);
        return map;
    }

}
